package com.anysoft.util;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 支持多个timebucket
 *
 * @since 1.6.13.7 [20200720 duanyy] <br>
 */
public final class TimeBucketGroup {
    private List<TimeBucket> buckets = new ArrayList<TimeBucket>();

    public TimeBucketGroup(String patternList){
        String [] list = patternList.split(";");
        for (String pattern:list){
            if (StringUtils.isNotEmpty(pattern)) {
                buckets.add(new TimeBucket(pattern));
            }
        }
    }

    public void addTimeBucket(String pattern){
        if (TimeBucket.check(pattern)){
            buckets.add(new TimeBucket(pattern));
        }
    }

    public void addTimeBucket(TimeBucket bucket){
        buckets.add(bucket);
    }

    public boolean match(long now){
        for (TimeBucket bucket:buckets){
            if (bucket.match(now)){
                return true;
            }
        }

        return false;
    }
}
